package com.erp.client.widget;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.StatusButtonBar;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.erp.client.DAO.ServerUserBaseAsync;
import com.erp.client.DAO.SeriaRzLog;
import com.erp.client.MessAge.myMesswindow;
import com.erp.client.systemEntryPoint;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.TimeField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import java.util.Date;

public class myInsertRzlogDialog extends Dialog {

    protected Button reset;
    protected Button save;
    protected FormPanel form;
    protected StatusButtonBar buttonBar;
    protected ServerUserBaseAsync customerService;
    protected BeanModel customerModel;
    protected ListStore<BeanModel> store;
    final private myMesswindow messdisp = new myMesswindow();

    public myInsertRzlogDialog(final ServerUserBaseAsync customerService, final ListStore<BeanModel> store, final SeriaRzLog seriarzlog) {
        this.customerService = customerService;
        this.store = store;

        FormData formData = new FormData("100%");

        FormLayout layout = new FormLayout();
        layout.setLabelWidth(90);
        layout.setDefaultWidth(155);
        setLayout(layout);

        setButtons("");
        setIconStyle("user");
        setHeading("日 志");//"我的日志");
        setModal(true);
        setBodyBorder(true);
        setBodyStyle("padding: 8px;background: none");
        setSize(500, 320);
        setResizable(false);

        form = new FormPanel();
        form.setHeading("我的日志");
        form.setHeaderVisible(false);

        form.setFrame(true);
        form.setIconStyle("icon-form");
        form.setCollapsible(true);
        form.setSize(465, -1);
        form.setButtonAlign(HorizontalAlignment.CENTER);
        form.setLayout(new FlowLayout());

        LayoutContainer main = new LayoutContainer();
        main.setLayout(new ColumnLayout());

        LayoutContainer left = new LayoutContainer();
        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        left.setLayout(layout);

        final HiddenField<Integer> rzId = new HiddenField<Integer>();
        rzId.setName("rzId");
        rzId.setFieldLabel("rzId");
        // id.setVisible(false);
        left.add(rzId, formData);

        final HiddenField<Integer> empId = new HiddenField<Integer>();
        empId.setName("empId");
        empId.setFieldLabel("empId");
        // id.setVisible(false);
        left.add(empId, formData);



        final HiddenField<Integer> depId = new HiddenField<Integer>();
        depId.setName("depId");
        depId.setFieldLabel("depId");

        // id.setVisible(false);
        left.add(depId, formData);

        final HiddenField<String> depName = new HiddenField<String>();
        depName.setName("depName");
        depName.setFieldLabel("depName");
        // id.setVisible(false);
        left.add(depName, formData);

        final HiddenField<String> inputdate = new HiddenField<String>();
        inputdate.setName("inputDate");
        inputdate.setFieldLabel("inputDate");
        // id.setVisible(false);
        left.add(inputdate, formData);

        final HiddenField<Integer> mangeId = new HiddenField<Integer>();
        mangeId.setName("mangeId");
        mangeId.setFieldLabel("mangeId");
        // id.setVisible(false);
        left.add(mangeId, formData);

        final HiddenField<Double> mangeDemo = new HiddenField<Double>();
        mangeDemo.setName("mangeDemo");
        mangeDemo.setFieldLabel("mangeDemo");
        // id.setVisible(false);
        left.add(mangeDemo);


        final HiddenField<Double> mangeNumerical = new HiddenField<Double>();
        mangeNumerical.setName("mangeNumerical");
        mangeNumerical.setFieldLabel("mangeNumerical");
        // id.setVisible(false);
        left.add(mangeNumerical);

        final TextField<String> empname = new TextField<String>();
        empname.setName("empName");
        empname.setFieldLabel("姓名");
        empname.setAllowBlank(false);
        empname.setMinLength(2);
        empname.setMaxLength(10);
        empname.setReadOnly(true);
        empname.setEmptyText("请输入姓名");
        empname.setMessageTarget("姓名输入有错误！");
        left.add(empname, formData);

        final TimeField begintime = new TimeField();
        begintime.setName("beginTime");
        begintime.setFieldLabel("开始时间");
        begintime.setAllowBlank(false);
        begintime.setIncrement(5);
        begintime.setFormat(DateTimeFormat.getFormat("HH:mm"));
        begintime.setEmptyText("请输入开始时间");
        begintime.setMessageTarget("请输入开始时间");
        left.add(begintime, formData);



        LayoutContainer right = new LayoutContainer();
        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        right.setLayout(layout);

        final DateTimePropertyEditor propertyEditor = new DateTimePropertyEditor("yyyy-MM-dd");

        final DateField recdate = new DateField();
        recdate.setPropertyEditor(propertyEditor);
        recdate.setAutoValidate(true);
        recdate.setName("recDate");
        recdate.setFieldLabel("日 期");
        recdate.setAllowBlank(false);
        Date dt = new Date();
        recdate.setValue(dt);
        recdate.setEmptyText("请选择日期");
        recdate.setMessageTarget("请选择日期");

        right.add(recdate, formData);

        final TimeField endtime = new TimeField();
        endtime.setName("endTime");
        endtime.setFieldLabel("结束时间");
        endtime.setAllowBlank(false);
        endtime.setIncrement(5);
        endtime.setFormat(DateTimeFormat.getFormat("HH:mm"));
        endtime.setEmptyText("请输入结束时间");
        endtime.setMessageTarget("请输入日志内容");
        right.add(endtime, formData);


        main.add(left, new ColumnData(.5));
        main.add(right, new ColumnData(.5));

        form.add(main);

        LayoutContainer area = new LayoutContainer();
        area.setStyleAttribute("padding", "0 10px 5px 10px");

        layout = new FormLayout();
        layout.setLabelAlign(LabelAlign.TOP);
        layout.setPadding(0);
        area.setLayout(layout);

        final TextArea recdata = new TextArea();
        recdata.setName("recData");
        recdata.setFieldLabel("内容");
        recdata.setAllowBlank(false);
        recdata.setMinLength(3);
        recdata.setMaxLength(500);
        recdata.setHeight(100);
        recdata.setEmptyText("请输入日志内容");
        recdata.setMessageTarget("请输入日志内容");

        area.add(recdata, formData);

        form.add(area);

        customerModel = BeanModelLookup.get().getFactory(SeriaRzLog.class).createModel(seriarzlog);
        final FormBinding formBinding = new FormBinding(form, true);
        formBinding.bind(customerModel);
        customerModel.set("empId", systemEntryPoint.StaticEmpID);
        empId.setValue(systemEntryPoint.StaticEmpID);
        customerModel.set("depId", systemEntryPoint.StaticDepId);
        depId.setValue(systemEntryPoint.StaticDepId);
        customerModel.set("empName", systemEntryPoint.StaticUserName);
        empname.setValue(systemEntryPoint.StaticUserName);
        customerModel.set("depName", systemEntryPoint.StaticDepName);
        depName.setValue(systemEntryPoint.StaticDepName);
        add(form);

        buttonBar = new StatusButtonBar();
        reset = new Button("重置");
        reset.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                begintime.clearInvalid();
                begintime.clearState();
                endtime.clearInvalid();
                endtime.clearState();
                recdata.setValue("");
            }
        });

        save = new Button("保存");

        save.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                if (form.isValid()) {
                    // buttonBar.getStatusBar().showBusy("正在处理，请稍后...");
                    //buttonBar.disable();

                    final SeriaRzLog customer = customerModel.getBean();

                    customer.setBeginTime(Integer.toString(begintime.getValue().getHour()) + ":" + Integer.toString(begintime.getValue().getMinutes()));
                    customer.setEndTime(Integer.toString(endtime.getValue().getHour()) + ":" + Integer.toString(endtime.getValue().getMinutes()));
                    customerService.SaveRzlog(customer, new AsyncCallback() {

                        public void onFailure(Throwable caught) {
                            buttonBar.getStatusBar().hide();
                            messdisp.myMessWindow("提示信息", "保存失败！");
                            buttonBar.enable();
                        }

                        public void onSuccess(Object result) {
                            buttonBar.getStatusBar().hide();
                            store.add(customerModel);
                            store.getLoader().load();
                            messdisp.myMessWindow("提示信息", (String) result);
                            begintime.clearInvalid();
                            begintime.clearState();
                            endtime.clearInvalid();
                            endtime.clearState();
                            recdata.setValue("");
                            buttonBar.enable();
                            
                        }
                    });

                }
            }
        });

        buttonBar.add(save);
        buttonBar.add(reset);
        setButtonBar(buttonBar);

        show();


    }
}
